package com.cskaoyan.configuration.shiro;

import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * @author stone
 * @date 2023/06/16 14:40
 */
@Component
public class MarketSessionManager extends DefaultWebSessionManager {

    private static final String TOKEN = "X-CskaoyanMarket-Token";

    // 会话管理管理能够根据SessionId获得Session → 提供SessionId
    @Override
    protected Serializable getSessionId(ServletRequest servletRequest, ServletResponse response) {
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        String sessionId = request.getHeader(TOKEN);
        if (sessionId != null && sessionId.length() > 0) {
            return sessionId;
        }
        return super.getSessionId(servletRequest, response);
    }
}

